// pages/user/makeCheapShop/makeCheapShop.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isSelected:true,//判断哪个范例选中
    postData: { 'couponType': '0', 'startDate': '2018/01/01', 'endDate': '2018/01/01', 'ccounts': '0', 'content': '', 'moneys': '0','description':''},//请求数据
  },

  desInput: function (e) {
    var that = this;
    var data = that.data.postData;
    console.log(e.detail.value);
    data['description'] = e.detail.value;
    that.setData({
      postData: data,
    })
  },

  countInput: function (e) {
    var that = this;
    var data = that.data.postData;
    console.log(e.detail.value);
    data['ccounts'] = e.detail.value;
    that.setData({
      postData: data,
    })
  },

  //金钱输入
  moneyInput:function(e){
    var that = this;
    var data = that.data.postData;
    console.log(e.detail.value);
    data['moneys'] = e.detail.value;
    that.setData({
      postData: data,
    })
  },

  //内容输入
  inputClick:function(e){
    var that = this;
    var data = that.data.postData;
    console.log(e.detail.value);
    data['content'] = e.detail.value;
    that.setData({
      postData: data,
    })
  },

  bindDateChange: function (e) {
    console.log(e.currentTarget.id);
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var that = this;
    var data = that.data.postData;

    if (e.currentTarget.id ==1){
      data['startDate'] = e.detail.value.replace(/-/g, '/');
    }else{
      data['endDate'] = e.detail.value.replace(/-/g, '/');
    }
    that.setData({
      postData: data,
    })
  },

  //选择范例1或者2
  chooseType:function(e){
    var that = this;
    console.log(e.currentTarget.id);
    if(e.currentTarget.id == 1){
      that.setData({
        isSelected:true,
      })
    }else{
      that.setData({
        isSelected: false,
      })
    }
  },

  //生成优惠券
  makeCheapShop:function(){
    var that = this;
    var data = that.data.postData;
    data['couponType'] = that.data.isSelected ? '0' : '1';
    that.setData({
      postData: data,
    })
    
    app.post('coupon/add',that.data.postData).then((res) => {
      console.log('success');
      console.log(res);
      if (res.code == 0) {
        wx.showModal({
          title: '温馨提示',
          content: res.msg,
          showCancel: false,
          success(ress) {
            if (ress.confirm) {
              wx.navigateBack();
            }
          }
        })
      } else {
        wx.showModal({
          title: '温馨提示',
          content: res.msg,
          showCancel: false,
        })
      }
    }).catch((errMsg) => {
      console.log('fail');
      console.log(errMsg + '失败');
    })

    /*
    
       app.post('act/listForMy', {
      pageNum: that.data.pageNum, openId: wx.getStorageSync('userInfo').userId}).then((res) => {
      console.log('success');
      console.log(res);
      if (res.code == 0) {
        var result = res.resultData;
        for (var i = 0; i < result.length; i++) {
          result[i].img_url = result[i].img_url.split(',');
        }

        console.log(result);
        that.setData({
          listArr: that.data.listArr.concat(result),
        })
      } else {
        wx.showModal({
          title: '温馨提示',
          content: res.msg,
          showCancel: false,
        })
      }
    }).catch((errMsg) => {
      console.log('fail');
      console.log(errMsg + '失败');
    })
    */
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var data = that.data.postData;
    data['openId'] = wx.getStorageSync('userInfo').userId;
    that.setData({
      postData: data,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})